package com.cat.chapter049;

/**
 * @author cat
 * @description https://leetcode.cn/problems/minimum-size-subarray-sum/
 * @create 2025/7/18 19:06
 * @since JDK17
 */

public class Solution01 {
    public int minSubArrayLen(int target, int[] nums) {
        int l = 0, r = 0, sum = 0, n = nums.length, ans = Integer.MAX_VALUE;
        while (r < n) {
            while (r < n && sum < target) {  // sum >= target || r == n
                sum += nums[r++];
            }
            if (sum >= target) {
                ans = Math.min(ans, r - l);
                sum -= nums[l++];
            }
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}
